package com.fgs.common.widget.spinner;

import android.content.Context;

import com.fgs.common.interf.ISelectItemValue;
import com.xuexiang.xui.widget.spinner.materialspinner.MaterialSpinnerBaseAdapter;

import java.util.List;

/**
 * @author fgs
 * @date 2020/8/26
 * @description
 **/
public class PullSpinnerAdapter<T> extends MaterialSpinnerBaseAdapter<T> {
    private final List<T> mItems;

    public PullSpinnerAdapter(Context context, List<T> items) {
        super(context);
        mItems = items;
    }

    @Override
    public int getCount() {
        return mItems != null ? mItems.size() - 1 : 0;
    }

    /**
     * 获取列表项
     *
     * @param position
     * @return
     */
    @Override
    public T getItem(int position) {
        if (position >= getSelectedIndex()) {
            return mItems.get(position + 1);
        } else {
            return mItems.get(position);
        }
    }

    @Override
    public T get(int position) {
        if (mItems != null && position >= 0 && position <= mItems.size() - 1) {
            return mItems.get(position);
        } else {
            return null;
        }
    }

    @Override
    public String getItemText(int position) {
        Object object = getItem(position);
        if (object instanceof String) {
            return object.toString();
        } else if (object instanceof ISelectItemValue) {
            ISelectItemValue iSelectItemValue = (ISelectItemValue) object;
            return iSelectItemValue.getSelectItemText();
        }

        return super.getItemText(position);
    }

    @Override
    public List<T> getItems() {
        return mItems;
    }
}
